package com.example.selenium.core;

import com.example.selenium.AutomaticGrab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTaskForInvoice {

    @Autowired
    private AutomaticGrab automaticGrab;

    /**
     * 3.添加定时任务,每天凌晨3点钟，自动抓取发票信息和支付成功的运单信
     */
    // @Scheduled(cron = "0 0 3 * * ?")
    private void configureTasks() {
        System.err.println("定时任务---->自动抓取发票信息和支付成功的运单信息，当前时间[" + LocalDateTime.now()+"]");
        // 先调用接口，测试cookie是否有效
        try {
            automaticGrab.testCookieOnline();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String startTime = automaticGrab.getLastQueryTimeForInvoice();
        String endTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        try {
            automaticGrab.getOpenedInvoiceWaybill(startTime,endTime);
            System.err.println("定时任务更新完毕---->自动抓取发票信息和支付成功的运单信息，当前时间[" + LocalDateTime.now()+"]");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


}